using System;
using System.Text.RegularExpressions;

using DogfoodRegexes=Jitsu.RegexC.RegularExpressions;

class CommandLine
{
	public CommandLine(string[] args)
	{
		// Parse the args.
		foreach (string arg in args)
		{
			if (arg.StartsWith("/") || arg.StartsWith("-"))
			{
				LazyLoadSwitchPattern();

				if (!switchpattern.IsMatch(arg))
					ThrowUsage(arg);

				Match m = switchpattern.Match(arg);
				switch (m.Groups["switch"].Value.ToLower()[0])
				{
					case '?': throw new ArgumentException();
					case 'o': outputpath = m.Groups["outputpath"].Value; break;
					default: ThrowUsage(arg); break;
				}
			}
			else if (sourcepath == null)
				sourcepath = arg;
			else
				ThrowUsage(arg);
		}

		// Validate it.
		if (sourcepath == null)
			throw new ArgumentException();
	}

	private void ThrowUsage(string arg)
	{
		throw new ArgumentException("Unrecognized command line argument: "+arg);
	}

	public static int ShowUsage(string message)
	{
		Console.Error.WriteLine(message);
		Console.Error.WriteLine("\nUsage: regexc <sourcefile> [/out=<assemblypath>]");
		Console.Error.WriteLine("\nBy default, the output assembly is named after the source file");
		Console.Error.WriteLine("(extension replaced with .dll) and placed in the current directory.");
		return 1;
	}

	public string SourcePath
	{
		get { return sourcepath; }
	}
	string sourcepath;

	public string OutputPath
	{
		get { return outputpath; }
	}
	string outputpath;

	private static Regex switchpattern;
	private static void LazyLoadSwitchPattern()
	{
		if (switchpattern == null)
			switchpattern = new DogfoodRegexes.CommandLineSwitch();
	}
}